1 /*
2 * The Apache Software License, Version 1.1
3 *
4 * Copyright (c) 2002 The Apache Software Foundation. All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
30 *
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
53 */
54 package net.sf.cglib.reflect;
55
56 import java.lang.reflect.Constructor;
57 import java.lang.reflect.Method;
58 import java.util.*;
59 import junit.framework.*;
60
61 public class TestReflectPerf extends net.sf.cglib.CodeGenTestCase {
62 public interface IndexOf {
63 int indexOf(String s, int start);
64 }
65
66 public void testReflectPerf() throws Throwable {
67 int iterations = 1000000;
68 System.out.println();
69 System.out.println("iteration count: " + iterations);
70
71 String test = "abcabcabc";
72
73 Class[] types = new Class[]{ String.class, Integer.TYPE };
74 Method indexOf = String.class.getDeclaredMethod("indexOf", types);
75 FastClass fc = FastClass.create(String.class);
76 FastMethod fm = fc.getMethod("indexOf", types);
77 int fidx = fm.getIndex();
78 Object[] args = new Object[]{ "ab", new Integer(1) };
79
80 IndexOf fast = (IndexOf)MethodDelegate.create(test, "indexOf", IndexOf.class);
81
82 int result;
83 long t1 = System.currentTimeMillis();
84 for (int i = 0; i < iterations; i++) {
85 result = ((Integer)fc.invoke("indexOf", types, test, args)).intValue();
86 }
87 long t2 = System.currentTimeMillis();
88 for (int i = 0; i < iterations; i++) {
89 args = new Object[]{ "ab", new Integer(1) };
90 result = ((Integer)indexOf.invoke(test, args)).intValue();
91 }
92 long t3 = System.currentTimeMillis();
93 for (int i = 0; i < iterations; i++) {
94 result = ((Integer)indexOf.invoke(test, args)).intValue();
95 }
96 long t4 = System.currentTimeMillis();
97 for (int i = 0; i < iterations; i++) {
98 args = new Object[]{ "ab", new Integer(1) };
99 result = ((Integer)fm.invoke(test, args)).intValue();
100 }
101 long t5 = System.currentTimeMillis();
102 for (int i = 0; i < iterations; i++) {
103 result = ((Integer)fm.invoke(test, args)).intValue();
104 }
105 long t6 = System.currentTimeMillis();
106 for (int i = 0; i < iterations; i++) {
107 result = ((Integer)fc.invoke(fidx, test, args)).intValue();
108 }
109 long t7 = System.currentTimeMillis();
110 for (int i = 0; i < iterations; i++) {
111 result = fast.indexOf("ab", 1);
112 }
113 long t8 = System.currentTimeMillis();
114 for (int i = 0; i < iterations; i++) {
115 result = test.indexOf("ab", 1);
116 }
117 long t9 = System.currentTimeMillis();
118
119 System.out.println("fc = " + (t2 - t1)
120 + "\n" + "reflect+args = " + (t3 - t2)
121 + "\n" + "reflect = " + (t4 - t3)
122 + "\n" + "fm+args = " + (t5 - t4)
123 + "\n" + "fm = " + (t6 - t5)
124 + "\n" + "fc w/idx = " + (t7 - t6)
125 + "\n" + "delegate = " + (t8 - t7)
126 + "\n" + "raw = " + (t9 - t8));
127 }
128
129
130
131 public TestReflectPerf(String testName) {
132 super(testName);
133 }
134
135 public static void main(String[] args) {
136 junit.textui.TestRunner.run(suite());
137 }
138
139 public static Test suite() {
140 return new TestSuite(TestReflectPerf.class);
141 }
142 }
This page was automatically generated by Maven